package cn.newgrand.netcall.offlinefiletransfer;

import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.Plugin;

import cn.newgrand.smackx.packet.FileTransferCancel;
import cn.newgrand.smackx.packet.OfflineProxyPacket;
import cn.newgrand.smackx.provider.FileTransferCancelProvider;
import cn.newgrand.smackx.provider.FileTransferPermissionProvider;
import cn.newgrand.smackx.provider.OfflineProxyProvider;

public class OfflineFilePlugin implements Plugin {

	private OfflineFileProxyListener listener = new OfflineFileProxyListener();
	private OfflineFileCancelListener listener2 = new OfflineFileCancelListener();

	@Override
	public void initialize() {
		// TODO Auto-generated method stub
		OfflineProxyProvider opProvider = new OfflineProxyProvider();
		ProviderManager.getInstance().addIQProvider("fileproxy", "ng:iq:fileproxy", opProvider);
		FileTransferCancelProvider ftcProvider = new FileTransferCancelProvider();
		ProviderManager.getInstance().addIQProvider("iq", "ng:iq:filetransfercancel", ftcProvider);
		FileTransferPermissionProvider fpProvider = new FileTransferPermissionProvider();
		ProviderManager.getInstance().addIQProvider("query", "ng:iq:filetransferpermission", fpProvider);
		
		PacketTypeFilter filter = new PacketTypeFilter(OfflineProxyPacket.class);
		SparkManager.getConnection().addPacketListener(listener, filter);
		PacketFilter filter2= new AndFilter(new IQTypeFilter(IQ.Type.GET),new PacketTypeFilter(FileTransferCancel.class));
		SparkManager.getConnection().addPacketListener(listener2, filter2);
		invokeOfflineFile();
	}

	@Override
	public void shutdown() {
		// TODO Auto-generated method stub
		ProviderManager.getInstance().removeIQProvider("fileproxy", "ng:iq:fileproxy");
		SparkManager.getConnection().removePacketListener(listener);
	}

	@Override
	public boolean canShutDown() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void uninstall() {
		// TODO Auto-generated method stub
		
	}

	private void invokeOfflineFile(){
//        OfflineProxyPacket info = OfflineTransferManager.getOfflineFileInfo();
//    	String proxyIP ="";
//    	String proxyPort = "";
//    	String proxyJID = "";
//    	String from = info.getFrom();
//    	String to = info.getTo();
//    	OfflineProxyPacket.HostInfo hostinfo =info.getHostInfo();
//    	if(hostinfo!=null){
//			proxyJID = hostinfo.getProxyJID();
//			proxyIP = hostinfo.getProxyIP();
//			proxyPort = hostinfo.getProxyPort();
//			String fileName, fileSize, fileDesc, initiator, md5,dataString;
//			for(OfflineProxyPacket.FileInfo fi:info.getList()){
//    			fileName = fi.getFileName();
//    			fileSize = fi.getFileSize();
//    			fileDesc = fi.getFileDesc();
//    			initiator = fi.getInitiator();
//    			md5 = fi.getMd5();
//    			dataString = fi.getDataString();
//    			OfflineFileTransferRequest of = new OfflineFileTransferRequest(fileName, fileSize, fileDesc, initiator, md5, from,proxyIP,proxyPort,dataString);
//    			of.setSession("jsi_"+Math.abs(OfflineFileTransferRequest.randomGenerator.nextLong()));
//    			OfflineTransferManager.handleTransferRequest(of);
//			}
//    	}
		OfflineTransferManager.sendOfflineFilePacket();
	}
	
}
